export default {
  data() {
    return {
      loading: false,
      page: null,
      rows: [],
      tableHeight: 0,
      sum: [],
    };
  },
  created() {
    let pageHeight = document.documentElement.clientHeight; // 可用窗口的高度
    let height = Number(pageHeight - 310); // 计算完之后剩余table可用的高度
    if (this.tableHeightDiff) {
      height = height + this.tableHeightDiff;
    }
    this.tableHeight = height;
  },
  mounted() {
    this.page = this.$refs.page;
    if (this.page_num) {
      this.page.size = this.page_num;
    }
    this.fetch();
  },
  updated() {
    if (this.$refs.table && this.$refs.table.showSummary) {
      this.$nextTick(() => {
        this.$refs.table.doLayout();
      });
    }
  },
  methods: {
    fetch(page = false) {
      if (!this.uri.list) {
        return;
      }
      let params = this.$refs.search
        ? {
            ...this.$refs.search.conditions,
          }
        : {};
      if (this.page) {
        if (page) {
          this.page.num = page;
        }
        params.pageNo = this.page.num;
        // params.size = this.page.size;
      }
      this.loading = true;
      this.axios.post(this.uri.list, params).then((res) => {
        if (this.page) {
          const { items, totalCount } = res.data;
          this.rows = items;
          this.page.total = totalCount;
          // this.sum = sum;
        } else {
          this.rows = res.data.items || res.data.words;
        }
        this.loading = false;
      });
    },
    onAdd() {
      this.$refs.add.open();
    },
    getCurrentRow() {
      return new Promise((resolve) => {
        const row = this.$refs.table.currentRow;
        if (!row) {
          this.$message.warning("请先选择一条数据");
        } else {
          resolve(row);
        }
      });
    },
    onEdit(row) {
      this.$refs.edit.open(row);
    },
    onDel(data) {
      this.axios.post(this.uri.del, data).then((res) => {
        if (res.retCode != 0) {
          return this.$message.error(res.retMsg);
        }
        this.fetch(1);
        this.$message.success("操作成功!");
      });
    },
    // onDelete() {
    //   this.getCurrentRow().then((row) => {
    //     this.$refs.sm.open();
    //   });
    // },
    doDelete() {
      let path = this.resource.split("/").pop();

      this.getCurrentRow().then((row) => {
        this.axios.delete(`${path}/${row.id}`).then((res) => {
          this.$message.success("操作成功!");
          this.fetch();
        });
      });
    },
    onDel2(row) {
      let path = this.resource.split("/").pop();

      this.axios.delete(`${path}/${row.id}`).then((res) => {
        this.$message.success("操作成功!");
        this.fetch();
      });
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        const { property } = column;
        if (index === 0) {
          sums[index] = this.getJsx({
            page: "小计",
            all: "合计",
          });
          return;
        }
        if (this.sum_cols.includes(index)) {
          const value = this.getTotalValue(property, data, index);
          sums[index] = this.getJsx(value);
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
    /** 获取jsx */
    getJsx(value) {
      let jsx = null;
      jsx = (
        <p>
          <span>
            {value.page ? (
              value.page
            ) : (
              <span style="visibility: hidden;">0</span>
            )}
          </span>
          <br />
          <span>
            {value.all ? value.all : <span style="visibility: hidden;">0</span>}
          </span>
        </p>
      );
      return jsx;
    },

    /** 根据自己的需求计算合计之后的数据 */
    getTotalValue(property, data, index) {
      let page = 0;
      data.forEach((ele) => {
        page = page + Number(ele[property]);
      });
      page = page.toFixed(2);
      if (page.endsWith("00")) {
        page = page.slice(0, -3);
      }
      //   console.log(this.sum_cols);
      let sum_index = this.sum_cols.indexOf(index);
      return {
        page,
        all: this.sum[sum_index],
      };
    },
  },
};
